﻿//using Hyperion.ValueSerializers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace SmartRoute
{
	static class ObjectSerializeExpand
	{

		//private static Hyperion.Serializer mSerializer = null;
		//public static Hyperion.Serializer Serializer
		//{
		//	get
		//	{
		//		if (mSerializer == null)
		//			mSerializer = new Hyperion.Serializer();
		//		return mSerializer;
		//	}
		//}

		public static string GetMd5Hash(this string input)
		{
			using (MD5 md5 = MD5.Create())
			{
				return GetMd5Hash(input, md5);
			}
		}

		public static string GetMd5Hash(this string input, MD5 md5)
		{

			byte[] data = md5.ComputeHash(Encoding.UTF8.GetBytes(input));
			StringBuilder sBuilder = new StringBuilder();

			for (int i = 0; i < data.Length; i++)
			{
				sBuilder.Append(data[i].ToString("x2"));
			}
			return sBuilder.ToString();

		}

		public static bool VerifyMd5Hash(this string hash, string input)
		{
			using (MD5 md5 = MD5.Create())
			{
				string hashOfInput = GetMd5Hash(input, md5);
				StringComparer comparer = StringComparer.OrdinalIgnoreCase;
				if (0 == comparer.Compare(hashOfInput, hash))
				{
					return true;
				}
				else
				{
					return false;
				}
			}
		}

		public static void Serialize(this object data, System.IO.Stream stream)
		{
			ProtoBuf.Meta.RuntimeTypeModel.Default.Serialize(stream, data);
			//ValueSerializer ser = Serializer.GetSerializerByType(data.GetType());
			//ser.WriteValue(stream, data, Serializer.GetSerializerSession());


		}
		public static object Deserialize(this System.IO.Stream stream, int length, Type type)
		{
			//ValueSerializer ser = Serializer.GetSerializerByType(type);
			//return ser.ReadValue(stream, Serializer.GetDeserializerSession());

			return ProtoBuf.Meta.RuntimeTypeModel.Default.Deserialize(stream, null, type, length);
		}
		public static T Deserialize<T>(this System.IO.Stream stream, int length)
		{
			return (T)Deserialize(stream, length, typeof(T));
		}
	}
}
